package com.gxa.controller;

import com.gxa.common.R;
import com.gxa.common.ResponseData;
import com.gxa.pojo.Resume;
import com.gxa.pojo.statistics.*;
import com.gxa.service.TalentPoolService;
import com.gxa.utils.GenerateSequenceUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller     //修饰整个类，描述Controller的作用
//在  http://localhost:8080/swagger-ui.html  地址中查看下面的效果
@Api(value = "人才库储备",tags = "人才库储备接口API")   //  value：代表代码级描述  tags：代表页面级描述
@RequestMapping(value = "/talentPool/*")   //接口和前端传递数据方式
public class TalentPoolController {
    @Autowired
    private TalentPoolService talentPoolService;


    @RequestMapping(value = "/login" , method = RequestMethod.POST)   //接口和前端传递数据方式
    @ApiOperation(value = "登录接口",notes = "传递电话和密码",httpMethod = "POST", response = ResponseData.class)  //swagger对方法的详细描述
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏的请求，接收参数"),
            @ApiResponse(code=405,message="方法不允许，请求方式错误"),
            @ApiResponse(code=500,message="格式错误"),
            @ApiResponse(code=505,message="后台数据库错误")
    })
    @ResponseBody   //响应json数据给前端
    //登录接口
    public ResponseData login(@RequestBody Login login){
       return talentPoolService.login(login);
    }

    @RequestMapping(value = "/resume" , method = RequestMethod.POST)   //接口和前端传递数据方式
    @ApiOperation(value = "主界面接口",notes = "传递电话",httpMethod = "POST", response = MyResume.class)  //swagger对方法的详细描述
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏的请求，接收参数"),
            @ApiResponse(code=405,message="方法不允许，请求方式错误"),
            @ApiResponse(code=500,message="格式错误"),
            @ApiResponse(code=505,message="后台数据库错误")
    })
    @ResponseBody   //响应json数据给前端
    //主界面接口
    public R resume(@RequestBody MyResume mr , HttpServletRequest request){
        String uploadDistPath = request.getServletContext().getRealPath("");    //获取当前项目的绝对路径
        mr.setRe_refreshDate_string(uploadDistPath);
        return talentPoolService.resume(mr);
    }

    @RequestMapping(value = "/register" , method = RequestMethod.POST)   //接口和前端传递数据方式
    @ApiOperation(value = "注册接口",notes = "传递简历表对象",httpMethod = "POST", response = R.class)  //swagger对方法的详细描述
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏的请求，接收参数"),
            @ApiResponse(code=405,message="方法不允许，请求方式错误"),
            @ApiResponse(code=500,message="手机已注册"),
            @ApiResponse(code=505,message="后台数据库错误")
    })
    @ResponseBody   //响应json数据给前端
    //注册接口
    public R register(@RequestBody Resume resume){
        return talentPoolService.register(resume);
    }



    @RequestMapping(value = "/modifyPwd" , method = RequestMethod.POST)   //接口和前端传递数据方式
    @ApiOperation(value = "修改密码接口",notes = "传递这个人的ID、新密码、旧密码",httpMethod = "POST", response = ModifyPwd.class)  //swagger对方法的详细描述
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏的请求，接收参数"),
            @ApiResponse(code=405,message="方法不允许，请求方式错误"),
            @ApiResponse(code=500,message="旧密码错误"),
            @ApiResponse(code=505,message="后台数据库错误")
    })
    @ResponseBody   //响应json数据给前端
    //修改密码接口
    public R modifyPwd(@RequestBody ModifyPwd modifyPwd){
       return talentPoolService.modifyPwd(modifyPwd);
    }

    @RequestMapping(value = "/informationBasic" , method = RequestMethod.POST)   //接口和前端传递数据方式
    @ApiOperation(value = "基本资料接口",notes = "传递简历表对象",httpMethod = "POST", response = R.class)  //swagger对方法的详细描述
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏的请求，接收参数"),
            @ApiResponse(code=405,message="方法不允许，请求方式错误"),
            @ApiResponse(code=505,message="后台数据库错误")
    })
    @ResponseBody   //响应json数据给前端
    //基本资料接口
    public R informationBasic (@RequestBody MyResume mr){
        return talentPoolService.informationBasic(mr);
    }

    @RequestMapping(value = "/informationDetails" , method = RequestMethod.POST)   //接口和前端传递数据方式
    @ApiOperation(value = "详细资料接口",notes = "传递简历表对象",httpMethod = "POST", response = R.class)  //swagger对方法的详细描述
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏的请求，接收参数"),
            @ApiResponse(code=405,message="方法不允许，请求方式错误"),
            @ApiResponse(code=505,message="后台数据库错误")
    })
    @ResponseBody   //响应json数据给前端
    //详细资料接口
    public R informationDetails (@RequestBody MyResume mr){
        return talentPoolService.informationDetails(mr);
    }

    @RequestMapping(value = "/informationFile" , method = RequestMethod.POST,produces = "application/json;charset=utf-8")   //接口和前端传递数据方式
    @ApiOperation(value = "文件资料接口",notes = "传递 人才库文件上传 对象",httpMethod = "POST", response = TalentPoolFile.class)  //swagger对方法的详细描述
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏的请求，接收参数"),
            @ApiResponse(code=405,message="方法不允许，请求方式错误"),
            @ApiResponse(code=505,message="后台数据库错误")
    })
    @ResponseBody   //响应json数据给前端
    //文件资料接口
    public R informationFile ( TalentPoolFile tp , HttpServletRequest request){
        R r = new R();
        boolean flag = true;
        String re_s = "";
        String re_j = "";
        String re_i = "";

        if(tp.getRe_selfpic()==null || tp.getRe_selfpic().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中个人照片");
            return r;
        }
        if(tp.getRe_jointb()==null || tp.getRe_jointb().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中入职附件");
            return r;
        }
        if(tp.getRe_idcardaddress()==null || tp.getRe_idcardaddress().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中身份附件");
            return r;
        }
        if(tp.getResume_id()==null || "".equals(tp.getResume_id())){
            r.setCode(500);
            r.setMsg("前端忘记传递id");
            return r;
        }
        if(tp.getRe_refreshDate()==null){
            r.setCode(500);
            r.setMsg("前端忘记传递更新时间");
            return r;
        }
        if(tp.getRe_status()==null || "".equals(tp.getRe_status())){
            r.setCode(500);
            r.setMsg("前端忘记传递状态");
            return r;
        }
        if(tp.getToken()==null || "".equals(tp.getToken())){
            r.setCode(500);
            r.setMsg("前端忘记token");
            return r;
        }


        //正常上传
        String uploadDistPath = request.getServletContext().getRealPath("");    //获取当前项目的绝对路径
        String filePath = File.separator + "upload" + File.separator + "talentPool" + File.separator;             //文件夹upload
        String uploadPath = uploadDistPath+filePath;
        System.out.println(uploadPath);

        //在项目下创个文件夹upload
        File file = new File(uploadPath);
        if(!file.exists()){
            file.mkdirs();
        }

        //拿到文件
        MultipartFile multipartFile = tp.getRe_selfpic();           //个人照片
        String fileName = multipartFile.getOriginalFilename();      //个人照片名称
        System.out.println(fileName);

        MultipartFile multipartFile2 = tp.getRe_jointb();          //入职附件
        String fileName2 = multipartFile2.getOriginalFilename();    //入职附件名称
        System.out.println(fileName2);

        MultipartFile multipartFile3 = tp.getRe_idcardaddress();          //身份附件
        String fileName3 = multipartFile3.getOriginalFilename();    //身份附件名称
        System.out.println(fileName3);



        //上传文件名称：不能用原名称，需要生成一个唯一的名字2021102911282500140.jpg
        String dataName = GenerateSequenceUtil.generateSequenceNo();        //拿个时间戳
        String ext = fileName.substring(fileName.lastIndexOf("."));     //取后缀
        String fileRealName = "re_s"+dataName + ext;                               //唯一的名字re_s2021102911282500140.jpg
        System.out.println(fileRealName);
        //上传文件名称：不能用原名称，需要生成一个唯一的名字2021102911282500140.jpg
        String dataName2 = GenerateSequenceUtil.generateSequenceNo();        //拿个时间戳
        String ext2 = fileName2.substring(fileName2.lastIndexOf("."));     //取后缀
        String fileRealName2 = "re_j"+dataName2 + ext2;                               //唯一的名字re_j2021102911282500140.jpg
        System.out.println(fileRealName2);
        //上传文件名称：不能用原名称，需要生成一个唯一的名字2021102911282500140.jpg
        String dataName3 = GenerateSequenceUtil.generateSequenceNo();        //拿个时间戳
        String ext3 = fileName3.substring(fileName3.lastIndexOf("."));     //取后缀
        String fileRealName3 = "re_i"+dataName3 + ext3;                               //唯一的名字re_i2021102911282500140.jpg
        System.out.println(fileRealName3);

        //上传.jpg.png.gif
        String allowExt=".jpg|.png|.gif";
        if(allowExt.lastIndexOf(ext)==-1){
            r.setCode(502);
            r.setMsg("个人照片文件格式错误，允许.jpg.png.gif文件");
            return r;
        }
        if(allowExt.lastIndexOf(ext2)==-1){
            r.setCode(502);
            r.setMsg("入职附件文件格式错误，允许.jpg.png.gif文件");
            return r;
        }
        if(allowExt.lastIndexOf(ext3)==-1){
            r.setCode(502);
            r.setMsg("入职附件文件格式错误，允许.jpg.png.gif文件");
            return r;
        }


        //将图片的相对路径保存下来，以后传递给数据库。
        String targetDataBasePath = filePath+fileRealName;
        System.out.println("传递给数据库-头像图片"+"\t"+targetDataBasePath);
        re_s = re_s+targetDataBasePath;
        //将图片的相对路径保存下来，以后传递给数据库。
        String targetDataBasePath2 = filePath+fileRealName2;
        System.out.println("传递给数据库-入职图片"+"\t"+targetDataBasePath2);
        re_j = re_j+targetDataBasePath2;
        //将图片的相对路径保存下来，以后传递给数据库。
        String targetDataBasePath3 = filePath+fileRealName3;
        System.out.println("传递给数据库-身份图片"+"\t"+targetDataBasePath3);
        re_i = re_i+targetDataBasePath3;

        File newFile = new File(uploadPath + fileRealName);
        File newFile2 = new File(uploadPath + fileRealName2);
        File newFile3 = new File(uploadPath + fileRealName3);
        System.out.println("newFile=" + newFile);
        System.out.println("newFile2=" + newFile2);
        System.out.println("newFile3=" + newFile3);
        //上传
        try {
            FileCopyUtils.copy(multipartFile.getBytes(),newFile);
            FileCopyUtils.copy(multipartFile2.getBytes(),newFile2);
            FileCopyUtils.copy(multipartFile3.getBytes(),newFile3);
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
        }

        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(re_s);
        System.out.println(re_j);
        System.out.println(re_i);
        System.out.println("用户id为  "+tp.getResume_id());
        System.out.println("更新时间为  "+tp.getRe_refreshDate());
        System.out.println("状态为  "+tp.getRe_status());
        System.out.println("Token为  "+tp.getToken());

        MyResume mr = new MyResume();
        mr.setResume_id(tp.getResume_id());                     //id
        mr.setRe_refreshDate_string(tp.getRe_refreshDate());    //更新时间字符串
        mr.setRe_selfpic(re_s);
        mr.setRe_jointb(re_j);
        mr.setRe_idcardaddress(re_i);
        mr.setRe_status(tp.getRe_status());
        mr.setToken(tp.getToken());


        //调用service



        if(flag){
            //调用Service
            r.setCode(200);
            r.setMsg("成功");
        }else{
            r.setCode(500);
            r.setMsg("文件上传失败");
        }

        return talentPoolService.informationFile(mr);
    }



    @RequestMapping(value = "/queryNation" , method = RequestMethod.GET)   //接口和前端传递数据方式
    @ApiOperation(value = "查询民族接口",notes = "不传递值",httpMethod = "GET", response = Resume.class)  //swagger对方法的详细描述
    @ResponseBody   //响应json数据给前端
    //查询民族接口
    public R queryNation(){
        return talentPoolService.queryNation();
    }

    @RequestMapping(value = "/queryDuties" , method = RequestMethod.GET)   //接口和前端传递数据方式
    @ApiOperation(value = "查询职务接口",notes = "不传递值",httpMethod = "GET", response = Resume.class)  //swagger对方法的详细描述
    @ResponseBody   //响应json数据给前端
    //查询职务接口
    public R queryDuties(){
        return talentPoolService.queryDuties();
    }

    @RequestMapping(value = "/queryEducation" , method = RequestMethod.GET)   //接口和前端传递数据方式
    @ApiOperation(value = "查询学历接口",notes = "不传递值",httpMethod = "GET", response = Resume.class)  //swagger对方法的详细描述
    @ResponseBody   //响应json数据给前端
    //查询学历接口
    public R queryEducation(){
       return talentPoolService.queryEducation();
    }

}
